package com.ricky04.jdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 DROP TABLE IF EXISTS `channel`;
 CREATE TABLE `channel` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(200) DEFAULT NULL,
 `code` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


 */
@Repository
public class ChannelDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void AddChannel(String name,String code){
        String sql="insert into channel (name,code) values ('"+name+"','"+code+"')";
        jdbcTemplate.execute(sql);
    }

    /**
     * 需要在配置类中开启事务@EnableTransactionManagement
     * @param name
     * @param code
     */
    @Transactional
    public void AddChannel2(String name,String code){
        if (name.equals("理财")) {
            return;
        }
        String sql="insert into channel (name,code) values ('"+name+"','"+code+"')";
        jdbcTemplate.execute(sql);
    }
}
